import requests
from lxml import etree

for page in range(1,4):

    url = f"http://bang.dangdang.com/books/soaringsales/1-{page}"

    res = requests.get(url)

    # print(res.status_code)

    # 把爬到静态网页保存到本地
    # with open("static/html/dangdang.html","w",encoding="utf-8") as f:
    #     f.write(res.text)

    tree = etree.HTML(res.text)

    li_all = tree.xpath('//ul[@class="bang_list clearfix bang_list_mode"]/li')

    for li in li_all:
        title = li.xpath(".//div[@class='name']/a/text()")[0]
        # print(title)
        book_name = title.split("（")[0]
        # print(type(book_name))
        author = li.xpath(".//div[@class='publisher_info']/a/text()")[0]
        time_list = li.xpath(".//div[@class='publisher_info']/span/text()")
        time = time_list[0] if time_list else "无"
        price_x = li.xpath(".//div[@class='price']/p/span[@class='price_n']/text()")[0]
        price_y = li.xpath(".//div[@class='price']/p/span[@class='price_r']/text()")[0]
        print(f'出版时间：{time}\t现价：{price_x}\t原价：{price_y}\t作者：{author}\t书名：{book_name}\t')
    print(f"第{page}页爬取完毕")
print("爬取完毕")



# title_all = tree.xpath("//div[@class='name']/a/text()")
# # price_all = tree.xpath("//div[@class='price']/p[1]/span[1]/text()")
# for t in title_all:
#     title = t.split("（")[0]
#
#     print(f'书名：{title}\t')


# print(res.text)


